/* md4.c
**
** Copyright (c) 2007, Meadhbh S. Hamrick
** All rights reserved.
** 
** Redistribution  and  use  in  source  and  binary  forms,  with  or  without
** modification, are permitted provided that the following conditions are met:
** 
**  * Redistributions of  source code must  retain the above  copyright notice,
**    this list of conditions and the following disclaimer.
** 
**  * Redistributions in binary form must reproduce the above copyright notice,
**    this list of conditions and the following disclaimer in the documentation
**    and/or other materials provided with the distribution.
** 
**  * Neither the  name of  Project Meadhbh  nor the names of  its contributors
**    may be used  to endorse  or promote  products derived from this  software
**    without specific prior written permission.
** 
** THIS SOFTWARE IS PROVIDED BY  THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY  EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT  LIMITED TO, THE
** IMPLIED WARRANTIES  OF MERCHANTABILITY AND FITNESS FOR  A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN NO  EVENT SHALL THE  COPYRIGHT OWNER OR  CONTRIBUTORS BE
** LIABLE  FOR  ANY  DIRECT,   INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
** CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT  LIMITED  TO,  PROCUREMENT  OF
** SUBSTITUTE GOODS  OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
** INTERRUPTION)  HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN
** CONTRACT,  STRICT LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE OR  OTHERWISE)
** ARISING IN ANY WAY  OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
** 
** $Id: md4.c 50 2007-08-31 08:11:28Z msh.mobile $
*/

/** \file md4.c
 ** This file implements the MD4 cryptographic hash function.
 */

/* Macro Definitions */

#ifndef MC_ROTATE_LEFT
#define MC_ROTATE_LEFT(x,n,b) (((x)<<(n))|((x)>>(b-n)))
#endif

#define MC_MIC_MD4_F(x,y,z) (((x)&(y))|((~x)&(z)))
#define MC_MIC_MD4_G(x,y,z) (((x)&(y))|((x)&(z))|((y)&(z)))
#define MC_MIC_MD4_H(x,y,z) ((x)^(y)^(z))

#define MC_MIC_MD4_ROUND1(a,b,c,d,x,k,s) ( ( a ) = MC_ROTATE_LEFT( ( ( a ) + MC_MIC_MD4_F( (b), (c), (d) ) + ( x[k] ) ), s, 32) )
#define MC_MIC_MD4_ROUND2(a,b,c,d,x,k,s) ( ( a ) = MC_ROTATE_LEFT( ( ( a ) + MC_MIC_MD4_G( (b), (c), (d) ) + ( x[k] ) + 0x5A827999 ), s, 32) )
#define MC_MIC_MD4_ROUND3(a,b,c,d,x,k,s) ( ( a ) = MC_ROTATE_LEFT( ( ( a ) + MC_MIC_MD4_H( (b), (c), (d) ) + ( x[k] ) + 0x6ED9EBA1 ), s, 32) )

/* File Includes */
#include "mutil.h"
#include "mcrypto/md4.h"

/* Static Prototypes */

static void mc_mic_md4_transform( tMCMd4 *context );

/* Function Definitions */

/** \fn tMCErr mc_mic_md4_clone( tMCMd4 *new_context, tMCMd4 *old_context )
**  \brief clone the state of an MD4 context
*/
void mc_mic_md4_clone( tMCMd4 *new_context, tMCMd4 *old_context ) {
  memcpy( new_context, old_context, sizeof( tMCMd4 ) );
}

/** \fn tMCErr mc_mic_md4_clear( tMCMd4 *context )
**  \brief clear sensitive data from the context
*/
void mc_mic_md4_clear( tMCMd4 *context ) {
  mc_mic_md4_initialize( context );
}

/** \fn tMCErr mc_mic_md4_initialize( tMCMd4 *context )
**  \brief initialize a MD4 context to it's initial condition
*/
void mc_mic_md4_initialize( tMCMd4 *context ) {
  context->count = 0;

  memset( context->data.asChar, 0, 64 );

  context->state[0] = 0x67452301;
  context->state[1] = 0xEFCDAB89;
  context->state[2] = 0x98BADCFE;
  context->state[3] = 0x10325476;
}
 
/** \fn tMCErr mc_mic_md4_update( tMCMd4 *context, uint8_t *data, uint32_t length )
**  \brief provide input to the hash function
*/

void mc_mic_md4_update( tMCMd4 *context, uint8_t *data, uint32_t length ) {
  uint32_t input_index;
  uint32_t block_index;

  for( input_index = 0; input_index < length; input_index ++ ) {

    /* copy data into the buffer */
    block_index = context->count % 64;
    context->data.asChar[ block_index ] = data[ input_index ];

    /* update the counter */
    context->count++;

    /* if the block buffer is full, transform the data */
    if( 63 == block_index ) {
      mc_mic_md4_transform( context );
    }
  }
}

/** \fn tMCErr mc_mic_md4_digest( tMCMd4 *context, uint8_t *digest )
**  \brief get the digest of the data provided
*/
void mc_mic_md4_digest( tMCMd4 *context, uint8_t *digest ) {
  uint32_t next_pad_index;
  uint32_t i;

  /* where does the first padding byte go? */
  next_pad_index = ( context->count % 64 );

  /* put the first padding byte */
  context->data.asChar[ next_pad_index++ ] = 0x80;

  /* are we past the 56th byte in the data buffer? */

  if( next_pad_index > 55 ) {
    mc_mic_md4_transform( context );
    next_pad_index = 0;
  }

  for( ; next_pad_index < 56; next_pad_index++ ) {
    context->data.asChar[ next_pad_index ] = 0;
  }

  /* append count to block */
#ifdef WORDS_BIGENDIAN
  context->data.asLongLong[ 7 ] = MU_END64(context->count * 8);
#else
  context->data.asLongLong[ 7 ] = context->count * 8;
#endif

  /* call transform */

  mc_mic_md4_transform( context );

  /* we may have to change state endianness */
#ifdef WORDS_BIGENDIAN
  for( i=0; i<4; i++ ) {
    context->state[i] = MU_END32( context->state[i] );
  }
#endif

  /* copy state to digest */
  memcpy( digest, (uint8_t *) context->state, 16 );
}

/** \fn void mc_mic_md4_transform( tMCMd4 *context )
**  \brief "churn" the internal state of the MD4 context based on input data
*/
static void mc_mic_md4_transform( tMCMd4 *context ) {
  uint32_t local_state[4];
  uint32_t i;

  /* save the state */
  local_state[0] = context->state[0];
  local_state[1] = context->state[1];
  local_state[2] = context->state[2];
  local_state[3] = context->state[3];

  /* depending on the endianness of the processor, we may have to change */
  /* the endianness of the data array                                    */

#ifdef WORDS_BIGENDIAN
  for( i = 0; i < 16; i++ ) {
    context->data.asInt[i] = ( uint32_t ) MU_END32( context->data.asInt[i] );
  } 
#endif

  /* round 1 */
  MC_MIC_MD4_ROUND1(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 0, 3 );
  MC_MIC_MD4_ROUND1(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 1, 7 );
  MC_MIC_MD4_ROUND1(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 2, 11 );
  MC_MIC_MD4_ROUND1(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 3, 19 );
  MC_MIC_MD4_ROUND1(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 4, 3 );
  MC_MIC_MD4_ROUND1(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 5, 7 );
  MC_MIC_MD4_ROUND1(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 6, 11 );
  MC_MIC_MD4_ROUND1(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 7, 19 );
  MC_MIC_MD4_ROUND1(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 8, 3 );
  MC_MIC_MD4_ROUND1(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 9, 7 );
  MC_MIC_MD4_ROUND1(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 10, 11 );
  MC_MIC_MD4_ROUND1(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 11, 19 );
  MC_MIC_MD4_ROUND1(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 12, 3 );
  MC_MIC_MD4_ROUND1(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 13, 7 );
  MC_MIC_MD4_ROUND1(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 14, 11 );
  MC_MIC_MD4_ROUND1(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 15, 19 );

  /* round 2 */
  MC_MIC_MD4_ROUND2(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 0, 3 );
  MC_MIC_MD4_ROUND2(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 4, 5 );
  MC_MIC_MD4_ROUND2(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 8, 9 );
  MC_MIC_MD4_ROUND2(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 12, 13 );
  MC_MIC_MD4_ROUND2(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 1, 3 );
  MC_MIC_MD4_ROUND2(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 5, 5 );
  MC_MIC_MD4_ROUND2(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 9, 9 );
  MC_MIC_MD4_ROUND2(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 13, 13 );
  MC_MIC_MD4_ROUND2(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 2, 3 );
  MC_MIC_MD4_ROUND2(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 6, 5 );
  MC_MIC_MD4_ROUND2(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 10, 9 );
  MC_MIC_MD4_ROUND2(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 14, 13 );
  MC_MIC_MD4_ROUND2(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 3, 3 );
  MC_MIC_MD4_ROUND2(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 7, 5 );
  MC_MIC_MD4_ROUND2(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 11, 9 );
  MC_MIC_MD4_ROUND2(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 15, 13 );

  /* round 3 */
  MC_MIC_MD4_ROUND3(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 0, 3 );
  MC_MIC_MD4_ROUND3(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 8, 9 );
  MC_MIC_MD4_ROUND3(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 4, 11 );
  MC_MIC_MD4_ROUND3(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 12, 15 );
  MC_MIC_MD4_ROUND3(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 2, 3 );
  MC_MIC_MD4_ROUND3(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 10, 9 );
  MC_MIC_MD4_ROUND3(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 6, 11 );
  MC_MIC_MD4_ROUND3(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 14, 15 );
  MC_MIC_MD4_ROUND3(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 1, 3 );
  MC_MIC_MD4_ROUND3(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 9, 9 );
  MC_MIC_MD4_ROUND3(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 5, 11 );
  MC_MIC_MD4_ROUND3(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 13, 15 );
  MC_MIC_MD4_ROUND3(
     context->state[0], context->state[1],
     context->state[2], context->state[3],
     context->data.asInt, 3, 3 );
  MC_MIC_MD4_ROUND3(
     context->state[3], context->state[0],
     context->state[1], context->state[2],
     context->data.asInt, 11, 9 );
  MC_MIC_MD4_ROUND3(
     context->state[2], context->state[3],
     context->state[0], context->state[1],
     context->data.asInt, 7, 11 );
  MC_MIC_MD4_ROUND3(
     context->state[1], context->state[2],
     context->state[3], context->state[0],
     context->data.asInt, 15, 15 );

  /* add the local_state back into the context state */
  context->state[ 0 ] += local_state[ 0 ];
  context->state[ 1 ] += local_state[ 1 ];
  context->state[ 2 ] += local_state[ 2 ];
  context->state[ 3 ] += local_state[ 3 ];

  memset( context->data.asChar, 0, 64 );

}
